package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 管理员日志
func (*DataParsing) parsingBoxLog(catData []string, ftpInfoId int64) []model.DataBox {
	var logDate, LogTime, steamId, gameName, gameId, boxId, boxType, dataTxt, newSteamId, newGameName, newGameId, location string
	var end int
	var dataArr []model.DataBox
	for _, logTxt := range catData {

		boxType = ""
		steamId = ""
		gameName = ""
		gameId = ""
		boxId = ""
		boxType = ""
		dataTxt = ""
		newSteamId = ""
		newGameName = ""
		newGameId = ""
		location = ""
		//fmt.Println("原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//获取箱子id
		start := strings.Index(logTxt, "id: ")
		//fmt.Println("位置1", start)
		if start < 0 {
			boxId = ""
		} else {
			end = strings.Index(logTxt[start+4:], ")")
			if end < 0 {
				boxId = ""
			} else {
				boxId = logTxt[start+4 : start+4+end]
			}
		}

		//获取数据类型
		start = strings.Index(logTxt, ") ")
		//fmt.Println("位置1", start)
		if start < 0 {
			boxType = ""
		} else {
			end = strings.Index(logTxt[start+2:], ".")
			if end < 0 {
				boxType = ""
			} else {
				boxType = logTxt[start+2 : start+2+end]
			}
		}

		if boxType == "ownership claimed" {
			//上锁
			start = strings.Index(logTxt, "Own")
			//fmt.Println("位置1", start)
			if start < 0 {
				dataTxt = ""
			} else {

				end = strings.Index(logTxt[start+3:], ".")
				if end < 0 {
					dataTxt = ""
				} else {
					dataTxt = logTxt[start+3 : start+3+end]
				}
			}

		} else {
			//上锁
			start = strings.Index(logTxt, "own")
			//fmt.Println("位置1", start)
			if start < 0 {
				dataTxt = ""
			} else {

				end = strings.Index(logTxt[start+3:], "-")
				if end < 0 {
					dataTxt = ""
				} else {
					dataTxt = logTxt[start+3 : start+3+end]
				}
			}
		}
		//fmt.Println("logTxt", logTxt)
		//获取箱子原主人steamId
		start = strings.Index(dataTxt, ": ")
		//fmt.Println("位置1", start)
		if start < 0 {
			steamId = ""
		} else {
			end = strings.Index(dataTxt[start+2:], " (")
			if end < 0 {
				steamId = ""
			} else {
				steamId = dataTxt[start+2 : start+2+end]
			}
		}

		//获取箱子原主人steamId
		start = strings.Index(dataTxt, " (")
		//fmt.Println("位置1", start)
		if start < 0 {
			gameId = ""
		} else {
			end = strings.Index(dataTxt[start+2:], ",")
			if end < 0 {
				gameId = ""
			} else {
				gameId = dataTxt[start+2 : start+2+end]
			}
		}

		//获取箱子原主人名称
		start = strings.Index(dataTxt, ", ")
		//fmt.Println("位置1", start)
		if start < 0 {
			gameName = ""
		} else {
			end = strings.Index(dataTxt[start+2:], ")")
			if end < 0 {
				gameName = ""
			} else {
				gameName = dataTxt[start+2 : start+2+end]
				gameName = TruncateStringByCharCount(gameName, 50)

				// 输出字节集（以十六进制表示）
				//fmt.Printf("Byte slice: %x\n", byteSlice)

				// 将字节集转换回字符串
				gameName = filterInvalidChars(gameName)
			}
		}

		if boxType != "ownership claimed" {
			//截取箱子新主人数据
			//上锁
			start = strings.Index(logTxt, "New ow")
			//fmt.Println("位置1", start)
			if start < 0 {
				dataTxt = ""
			} else {

				end = strings.Index(logTxt[start+6:], ".")
				if end < 0 {
					dataTxt = ""
				} else {
					dataTxt = logTxt[start+6 : start+6+end]
				}
			}

			//获取箱子新主人steamId
			start = strings.Index(dataTxt, ": ")
			//fmt.Println("位置1", start)
			if start < 0 {
				newSteamId = ""
			} else {
				end = strings.Index(dataTxt[start+2:], " (")
				if end < 0 {
					newSteamId = ""
				} else {
					newSteamId = dataTxt[start+2 : start+2+end]
				}
			}

			//获取箱子新主人steamId
			start = strings.Index(dataTxt, " (")
			//fmt.Println("位置1", start)
			if start < 0 {
				newGameId = ""
			} else {
				end = strings.Index(dataTxt[start+2:], ",")
				if end < 0 {
					newGameId = ""
				} else {
					newGameId = dataTxt[start+2 : start+2+end]
				}
			}

			//获取箱子新主人名称
			start = strings.Index(dataTxt, ", ")
			//fmt.Println("位置1", start)
			if start < 0 {
				newGameName = ""
			} else {
				end = strings.Index(dataTxt[start+2:], ")")
				if end < 0 {
					newGameName = ""
				} else {
					newGameName = dataTxt[start+2 : start+2+end]
					gameName = TruncateStringByCharCount(gameName, 50)

					// 输出字节集（以十六进制表示）
					//fmt.Printf("Byte slice: %x\n", byteSlice)

					// 将字节集转换回字符串
					gameName = filterInvalidChars(gameName)
				}
			}
		}

		start = strings.Index(logTxt, "tion: ")
		if start < 0 {
			location = ""
		} else {
			// 查找第二个单引号的位置
			location = logTxt[start+6:]
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, location)

		gameId, _ := strconv.ParseInt(gameId, 10, 64)
		newGameId, _ := strconv.ParseInt(newGameId, 10, 64)
		downloadFile := model.DataBox{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime,
			SteamId: steamId, GameName: gameName, GameId: gameId, BoxId: boxId, Type: boxType, NewGameId: newGameId, NewSteamId: newSteamId, NewGameName: newGameName, Location: location}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}
